package cn.crism.blog.service.impl;

import cn.crism.blog.entity.User;
import cn.crism.blog.param.RePwdParam;
import cn.crism.blog.repository.UserRepository;
import cn.crism.blog.service.UserService;
import cn.crism.blog.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Crism
 * @Date: 上午8:43 18/3/18
 * @Description:
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public User existUser(User user) {
        if (user.getUserName()!=null && user.getUserPwd()!=null){
            User u = userRepository.findByUserNameAndUserPwd(user.getUserName(), MD5Util.md5(user.getUserPwd()));
            if (u!=null) {
                u.setUserPwd(null);
                return u;
            }
        }
        return null;
    }

    @Override
    public Boolean modifyPwd(RePwdParam rePwdParam, User user) {
        if (user == null)
            return false;
        User u = userRepository.findByUserNameAndUserPwd(user.getUserName(),MD5Util.md5(rePwdParam.getUserPwd()));
        if (u!=null){
            try{
                u.setUserPwd(MD5Util.md5(rePwdParam.getNewPwd()));
                userRepository.save(u);
                return true;
            }catch (Exception e){
                return false;
            }
        }
        return false;
    }
}
